package GUI;

import java.util.ArrayList;

import processing.core.PImage;

public class HoursPanel {
	public int x;
	public int y;
	public int width;
	public int height;
	public ScrollBar scrollBar;
	public ArrayList<CloseButton> shapesButtons;
	public PlusButton pb;
	public Button graphButton;
	public Button showAirportsButton;
	public Button showMilitaryBasesButton;
	public Button showWeatherStationsButton;
	public Button showMeteorsButton;
	public Button clusterButton;
	public Button questionButton;
	public float buttonTimer;
	public int heightVal;
	public boolean shapesDrawn;

	public final int MAX_SHAPES = 10;
	public final int SHAPE_HEIGHT = 25;

	public HoursPanel() {
		super();
		this.x = GV.width-320;
		this.y = 90;
		this.width = 310;
		this.height = 30;
		this.shapesButtons = new ArrayList<CloseButton>();
		this.pb = new PlusButton("Shapes Selected");
		this.pb.x=GV.width-18;
		this.pb.y=70;
		this.scrollBar = new ScrollBar();

		for(int i=0;i<GV.shapes.size();i++){
			CloseButton b = new CloseButton();
			b.label = GV.shapes.get(i).getName();
			shapesButtons.add(b);
		}

		graphButton = new Button("Graphs",770,65);
		showAirportsButton = new Button("Airports",770,5);
		showMilitaryBasesButton = new Button("Military bases",655,5);
		showWeatherStationsButton = new Button("Weather St.",655,35);
		showMeteorsButton = new Button("Meteors",770,35);
		clusterButton = new Button("Cluster",655,65);
		questionButton = new Button("?",735,65);
	}

	public void draw(float dt)
	{

		drawBackGroud();

		if(GV.shapesSelected.size()>0){
			GV.processing.rectMode(GV.processing.CORNER);
			GV.processing.fill(GV.backGroundColor);
			GV.processing.stroke(80);
			GV.processing.strokeWeight(2);
			GV.processing.rect(pb.x- GV.processing.textWidth(pb.label) -15, pb.y-5, GV.processing.textWidth(pb.label)+10, pb.height*2-5);
			pb.draw();
		}
		clusterButton.draw();
		if(clusterButton.state){
			showAirportsButton.state=false;  showMeteorsButton.state=false;
			showMilitaryBasesButton.state=false;showWeatherStationsButton.state=false;
		}
		graphButton.draw();
		showAirportsButton.draw();
		showMilitaryBasesButton.draw();
		showWeatherStationsButton.draw();
		showMeteorsButton.draw();
		questionButton.draw();
		if(questionButton.state){
			ShowInfo();
		}


		if(pb.clicked && GV.shapesSelected.size()!=0){
			if(buttonTimer==(float)0.0){
				buttonTimer = GV.timer;
				heightVal=0;
				shapesDrawn=true;
			}
			heightVal = (int)GV.processing.lerp(heightVal,height,(float) 0.3);
			drawShapesSelectedBar();
		}
		else{if(buttonTimer!=(float)0.0){
			heightVal=height;
		}
		heightVal = (int)GV.processing.lerp(heightVal,0,(float) 0.3);
		drawShapesSelectedBar();
		buttonTimer=(float)0.0;

		}
	}

	private void ShowInfo() {
		GV.processing.strokeWeight(2);
		GV.processing.rectMode(GV.processing.CORNER);
		GV.processing.fill(GV.backGroundColor);
		GV.processing.stroke(80);
		GV.processing.rect(0,90,GV.width,GV.height-148);
		GV.processing.fill(GV.textColor);
		GV.processing.textSize(25);
		GV.processing.textAlign(GV.processing.CENTER);
		//GV.processing.text("",385,281);
		PImage image =GV.processing.loadImage("ufoIncon3.png");
		image.resize(288, 93);
		GV.processing.image(image, 512-288/2, 241);
		GV.processing.text("It Came Out of the Sky",512,180);
		GV.processing.textSize(18);
		GV.processing.textAlign(GV.processing.LEFT);
		GV.processing.text("Authors:",65,421);

		GV.processing.textSize(15);
		GV.processing.text("Vivek Hariharan",70,446);
		GV.processing.text("Giovanni F. Del Nero",70,471);
		
		
		GV.processing.textSize(18);
		int dist =505;
		GV.processing.text("Data:",GV.width - dist-5,421);
		
		GV.processing.textSize(15);
		GV.processing.text("Ufo sightings: http://www.nuforc.org/webreports/ndxloc.html",GV.width - dist,446);
		GV.processing.text("Airports: www.partow.net/miscellaneous/airportdatabase/",GV.width - dist,471);
		GV.processing.text("Military Bases: http://benefits.military.com/misc/installations/",GV.width - dist,496);
		GV.processing.text("Meteors: http://www.amsmeteors.org/fireballs/",GV.width - dist,521);
		GV.processing.text("Weather Stations: http://weather.rap.ucar.edu/surface/stations.txt",GV.width - dist,546);

		
		GV.processing.textSize(18);
		GV.processing.text("Developed with:",65,531);
		
		GV.processing.textSize(15);
		GV.processing.text("Processing: http://processing.org/",70,556);
		
	}

	private void drawShapesSelectedBar(){

		if(heightVal==0)return;

		int size =GV.shapesSelected.size();
		if(size<MAX_SHAPES)
			this.height= size*SHAPE_HEIGHT+5;
		else this.height=SHAPE_HEIGHT*MAX_SHAPES;


		GV.processing.rectMode(GV.processing.CORNER);
		GV.processing.fill(GV.backGroundColor,160);
		GV.processing.stroke(80);
		GV.processing.strokeWeight(2);
		GV.processing.rect(x+180, y, width, heightVal);
		GV.processing.textFont(GV.plotFont, 14);
		GV.processing.fill(GV.textColor);	
		GV.processing.textAlign(GV.processing.LEFT);

		float j;
		if(size>MAX_SHAPES &&heightVal>20){
			this.scrollBar.width = 15;
			this.scrollBar.x = this.x+this.width-5;
			this.scrollBar.y = y+5;
			this.scrollBar.height = this.heightVal-15;
			this.scrollBar.draw();  
			this.scrollBar.size = 1/(float)((float)size/(MAX_SHAPES-1));
			j = Main.map(this.scrollBar.val,0,1,0,GV.shapesSelected.size());
		}
		else j=0;
		int start = (int)(j+0.5);
		for(int i=start; i<GV.shapesSelected.size() && i<=start+MAX_SHAPES+2 && i<start+heightVal/24;i++){
			CloseButton b = shapesButtons.get(GV.shapesSelected.get(i).getShapeIndex().ordinal());
			if(b.clicked){
				this.scrollBar.val=0;
				b.clicked=false;
				GV.shapesSelected.remove(i);
				GV.shapesPanel.updateShapesPanelButtonsState();
			}
			else{
				b.index=i-start;
				b.x=x+20+170;
				b.y=y+15+(i-start)*24;
				b.draw();
				if(GV.shapeCategoryColor)
					GV.processing.fill(GV.shapeCol[GV.shapesSelected.get(i).getShapeIndex().ordinal()]);
				else 
					GV.processing.fill(GV.categoryCol[GV.shapesSelected.get(i).getCategory().ordinal()]);
				GV.processing.stroke(0);
				GV.processing.ellipse(b.x+100, b.y+5,12,12);
			}
		}

	}

	private void drawBackGroud() {
		GV.processing.strokeWeight(2);
		GV.processing.rectMode(GV.processing.CORNER);
		GV.processing.fill(GV.backGroundColor);
		GV.processing.stroke(80);
		GV.processing.rect(650,0,1024-650,y);
		GV.processing.line(650,65,GV.width,65);
		GV.processing.line(650+187,0,650+187,y);

	}

	public boolean mouseOver()
	{
		if(GV.processing.mouseX > x && GV.processing.mouseX < x + width && 
				GV.processing.mouseY > y && GV.processing.mouseY < y + height) 	return true;
		return false;
	}
	public boolean mouseOverArea()
	{
		if(GV.processing.mouseX > 770 && GV.processing.mouseX <  GV.width && 
				GV.processing.mouseY > 0 && GV.processing.mouseY < y ) 	return true;
		return false;
	}











}
